package com.bonc.web.aspect;

import com.bonc.util.request.ServletUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 操作日志记录
 * @Author: ChenZhiXiang
 * @CreateDate: 2018/10/11 0011 10:39
 * @Version: 1.0
 */


@Aspect
@Component
public class WebLogAspect {

    private static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.bonc.controller..*.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容
       HttpServletRequest request = ServletUtils.getRequest();


    }

    /*@AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
        logger.info("RESPONSE : " + ret);
        JSONObject jsonObject = JSONObject.parseObject((String) ret);
        System.out.println(" 测试" + jsonObject.get("respCode"));
        //代码为1001,1002,1003,0都算访问失败，其他算成功
        String[] num = new String[]{"1001","1002","1003","0"};
        logger.info(jsonObject.containsKey("respCode")+"-----"+jsonObject.containsKey("czx"));
        if (jsonObject.containsKey("respCode") || jsonObject.containsKey("success")){
            if (jsonObject.get("success")=="true" || !useLoop(num,String.valueOf(jsonObject.get("respCode")))){
                sysLog.setFlag("成功");
            }else {
                sysLog.setFlag("失败");
            }
        }else {
            sysLog.setFlag("失败");
        }
        operationService.addLog(sysLog);
    }*/

   /* public static boolean useLoop(String[] ags,String value){
        for (int i = 0;i < ags.length;i++){
            if (ags[i].equals(value)){
                return true;
            }
        }
        return false;
    }*/

    public static String getIpAddr(HttpServletRequest request) {
        /*String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getHeader("WL-Proxy-Client-IP");
         }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
          }
   return ip; */
        if (request == null)
        {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
}
